package seGobangClient.uiSystem3D;
import com.sun.j3d.utils.geometry.Text2D;
import javax.media.j3d.*;
import javax.vecmath.*;

import com.sun.j3d.utils.universe.*;
import java.util.Timer;
import java.util.TimerTask;
import seGobangClient.IMessageBox;
import seGobangResources.PathRes;
/**
 *
 * @author plus
 */
public class MessageBox3D implements IMessageBox{
    private static BranchGroup PGRoot, BGTemp;
    private static BehaviorFactory Behaviors;
    private static String CallBackName;
    
    private void initBrachGroup(){
        Behaviors=new BehaviorFactory();
        PGRoot=new BranchGroup();
        PGRoot.setCapability(BranchGroup.ALLOW_DETACH);
       try{
        TransformGroup TGMain=getTG((Node)null,0f,0f,-0.765f);
            TransformGroup TGMask=getTG(ObjectFactory.createTexturedSurface(
                0.64f, 0.48f, new Color3f(1,1,1), 0f, PathRes.getPath("Texture/MessageBox/Mask")),0f,0f,0f);
            TGMask.setPickable(false);
            TGMain.addChild(TGMask);
           TransformGroup TGBox=new TransformGroup();
           TransformGroup TGBack=getTG(ObjectFactory.createTexturedSurface(
                0.36f, 0.18f, new Color3f(1,1,1), 0.7f, PathRes.getPath("Texture/MessageBox/Box")),
                0f,0f,0.0f);
                TGBack.setPickable(false);
           TGBox.addChild(TGBack);
           TGBox.addChild(getTG(                                                //Title Group
                    new Text2D("                   ",new Color3f(1f,1f,1f),"微软雅黑",28,java.awt.Font.PLAIN),
                    -0.15f,.057f,0.0f));
           TransformGroup TGText=getTG(null,-0.1f,0f,0.0f);
           TGText.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
           TGText.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);
           TGBox.addChild(TGText);                        //Text Group
            
            TGBox.addChild(getTG(ObjectFactory.createTexturedSurface(
                    0.08f, 0.03f, new Color3f(1f,1f,1f), 0f, PathRes.getPath("Texture/MessageBox/YesB"),"YESB"),
                    ObjectFactory.createTexturedSurface(
                    0.08f, 0.03f, new Color3f(1f,1f,1f), 0f, PathRes.getPath("Texture/MessageBox/YesA"),"YESA"),
                    0.05f,-0.06f,0f));
            TGBox.addChild(getTG(ObjectFactory.createTexturedSurface(
                    0.08f, 0.03f, new Color3f(1f,1f,1f), 0f, PathRes.getPath("Texture/MessageBox/NoB"),"NOB"),
                    ObjectFactory.createTexturedSurface(
                    0.08f, 0.03f, new Color3f(1f,1f,1f), 0f, PathRes.getPath("Texture/MessageBox/NoA"),"NOA"),
                    0.135f,-0.06f,0f));
            TGBox.addChild(getTG(ObjectFactory.createTexturedSurface(
                    0.08f, 0.03f, new Color3f(1f,1f,1f), 0f, PathRes.getPath("Texture/MessageBox/OkB"),"OKB"),
                    ObjectFactory.createTexturedSurface(
                    0.08f, 0.03f, new Color3f(1f,1f,1f), 0f, PathRes.getPath("Texture/MessageBox/OkA"),"OKA"),
                    0.135f,-0.06f,0f));
            
        TGMain.addChild(TGBox);
        PGRoot.addChild(TGMain);
        
        Behavior PickMessageBox = Behaviors.createPickMessageBoxBehavior(PGRoot, TGMain, this);
            PickMessageBox.setSchedulingBounds(new BoundingSphere(new Point3d(0d,0d,0d),100));
        TGMain.addChild(PickMessageBox);
        
       }catch(Exception e){
           e.printStackTrace();
       }
        
        PGRoot.compile();
        
    }
private TransformGroup getTG(Node child,float x,float y,float z){
        Transform3D transRes = new Transform3D();                                
        transRes.set(new Vector3f(x, y, z));
        TransformGroup TGRes = new TransformGroup(transRes);
        TGRes.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        if (child!=null) TGRes.addChild(child);
        return TGRes;
    }
    private TransformGroup getTG(Node child1,Node child2,float x,float y,float z){
        Transform3D transRes = new Transform3D();                                
        transRes.set(new Vector3f(x, y, z));
        TransformGroup TGRes = new TransformGroup(transRes);
        TGRes.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        TGRes.setPickable(true);
        if (child1!=null) TGRes.addChild(child1);
        if (child2!=null) TGRes.addChild(child2);
        return TGRes;
    }
    static TransformGroup getTG(Text2D child,float x,float y,float z){
        Transform3D transRes = new Transform3D();                                
        transRes.set(new Vector3f(x, y, z));
        Transform3D transText=new Transform3D();
        transText.setScale(0.25);
        transRes.mul(transText);
        TransformGroup TGRes = new TransformGroup(transRes);
        if (child!=null) {
            ((Appearance)child.getAppearance()).setCapability(Appearance.ALLOW_TEXTURE_WRITE);
            TGRes.addChild(child);
        }
        return TGRes;
    }
    
    public int showMessageBox(int Type, String Title, String Text, String CallBackEventName) {
        setUpScene();
        TransformGroup TGBox=(TransformGroup)((TransformGroup)PGRoot.getChild(0)).getChild(1);
        TransformGroup TGText=(TransformGroup)TGBox.getChild(2);
        ((Text2D)((TransformGroup)TGBox.getChild(1)).getChild(0)).setString(Title);
        TGText.removeAllChildren();
        BranchGroup BGText=new BranchGroup();
        BGText.setCapability(BranchGroup.ALLOW_DETACH);
        Text=Text.replaceAll("<html>", "");
        Text=Text.replaceAll("</html>", "");
        String temp[]=Text.split("<br>");
        for (int i = 0; i < temp.length; i++) {
            //System.err.println(temp[i]);
            BGText.addChild(getTG(
                    new Text2D(temp[i], new Color3f(1f, 1f, 1f), "黑体", 96, java.awt.Font.PLAIN),
                    -0.15f, (float)(0.1-0.08*i), 0.0f));
        }
        TGText.addChild(BGText);
        
        CallBackName=CallBackEventName;
        
        Transform3D transEnableYes=new Transform3D(), transEnableNo=new Transform3D(), transDisable=new Transform3D();
        transEnableYes.set(new Vector3d(0.05,-0.06,0)); 
        transEnableNo.set(new Vector3d(0.135,-0.06,0));
        transDisable.set(new Vector3d(0.05,1,0));
        
        if (Type==TYPE_QUESTION){
            ((TransformGroup)TGBox.getChild(3)).setTransform(transEnableYes);
            ((TransformGroup)TGBox.getChild(4)).setTransform(transEnableNo);
            ((TransformGroup)TGBox.getChild(5)).setTransform(transDisable);
        }else{
            ((TransformGroup)TGBox.getChild(3)).setTransform(transDisable);
            ((TransformGroup)TGBox.getChild(4)).setTransform(transDisable);
            ((TransformGroup)TGBox.getChild(5)).setTransform(transEnableNo);
        }
        //((Plane2D)((TransformGroup)TGBox.getChild(0)).getChild(0)).setAlpha(0f);
        
        SceneManager.dashBoardRoot.addChild(PGRoot);
        //shutDown();
        return 1;
    }
    
    void Process(String res){
        if(res.startsWith("YES") || res.startsWith("OK")){
            shutDown();
            if (CallBackName!=null) SceneManager.MyCurrentController.doEvent(CallBackName, 1);
            //.bEnable=true;
        }
        if(res.startsWith("NO")){
            shutDown();
            if (CallBackName!=null) SceneManager.MyCurrentController.doEvent(CallBackName, 0);
            
        }
    }
    
    public void setUpScene() {
        if (PGRoot==null) initBrachGroup();
        SceneManager.bEnable=false;
            BGTemp=(BranchGroup)SceneManager.dashBoardRoot.getChild(1);
            //BGTemp.setCapability(BranchGroup.ALLOW_DETACH);
            BGTemp.detach();
           
       /* Transform3D t3d=new Transform3D();
        t3d.set(new Vector3f(0f,0f,-0.76f));
        ((TransformGroup)PGRoot.getChild(0)).setTransform(t3d);*/
    }

    public void shutDown() {
        SceneManager.bEnable=true;
        PGRoot.detach(); //与主场景脱离
        SceneManager.dashBoardRoot.addChild(BGTemp);
    }
    
}
